package com.ygs.secs.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ygs.secs.model.*;
import com.ygs.secs.service.EnterpriseJobService;
import com.ygs.secs.service.EnterpriseService;
import com.ygs.secs.service.JobTypeService;
import com.ygs.secs.service.ReplyService;
import com.ygs.secs.tools.CustomTools;
import com.ygs.secs.tools.Pinyin4jUtil;
import io.swagger.models.auth.In;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyEmitterReturnValueHandler;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 企业岗位模块
 */
@RequestMapping("/ygssecs")
@RestController
public class EnterpriseJobController {
    @Autowired
    JobTypeService jobTypeService;
    @Autowired
    EnterpriseJobService jobService;
    @Autowired
    EnterpriseService enterpriseService;
    @Autowired
    ReplyService replyService;


    /**
     * 获取岗位分类
     *
     * @return
     */
    @RequestMapping("/getjobType")
    public Map<String, Object> getjobType() {
        Map<String, Object> map = new HashMap<>();
        List<JobType> jobTypes = jobTypeService.getAlljobType();
        map.put("data", jobTypes);
        return map;
    }

    /**
     * 分页获取岗位类型
     *
     * @param page  页码
     * @param limit 每页条数
     * @return json
     */
    @RequestMapping("/getjobtypesforpages")
    public Map<String, Object> getjobtypesforpages(@RequestParam(defaultValue = "1") int page, int limit) {
        Map<String, Object> map = new HashMap<>();
        try {
            //分页查询
            Page startPage = PageHelper.startPage(page, limit);
            List<JobType> jobTypes = jobTypeService.getAlljobType();
            if (jobTypes != null) {
                map.put("code", 200);
                map.put("data", jobTypes);
                map.put("count", startPage.getTotal());
            } else {
                map.put("code", 201);
                map.put("data", null);
                map.put("msg", "获取数据失败");
            }
        } catch (Exception e) {
            map.put("code", 201);
            map.put("msg", "获取数据失败");
            map.put("data", null);
            e.printStackTrace();
        }
        return map;
    }

    /**
     * 添加岗位类型
     *
     * @param jobtypejson 岗位类型数据
     * @return json
     */
    @RequestMapping("/addjobtype")
    public Map<String, Object> addjobtype(String jobtypejson) {
        Map<String, Object> map = new HashMap<>();
        try {
            JobType jobType = JSON.parseObject(jobtypejson, JobType.class);
            int res = jobTypeService.insert(jobType);
            if (res > 0) {
                map.put("code", 200);
                map.put("msg", "操作成功");
            } else {
                map.put("code", 500);
                map.put("msg", "操作成功");
            }
        } catch (Exception e) {
            map.put("code", 501);
            map.put("msg", "system error");
        }
        return map;
    }

    /**
     * 批量添加岗位数据
     *
     * @param jsonArrayjobType 岗位json数据
     * @return json
     */
    @RequestMapping("/batchInsertjobtype")
    public Map<String, Object> batchInsertjobtype(String jsonArrayjobType) {
        Map<String, Object> map = new HashMap<>();
        try {
            List<JobType> jobTypeList = JSONArray.parseArray(jsonArrayjobType, JobType.class);
            int res = jobTypeService.batchInsert(jobTypeList);
            if (res > 0) {
                map.put("code", 200);
                map.put("msg", "操作成功");
            } else {
                map.put("code", 500);
                map.put("msg", "操作成功");
            }
        } catch (Exception e) {
            map.put("code", 501);
            map.put("msg", "system error");
        }
        return map;
    }

    /**
     * 批量删除岗位类型
     *
     * @param jobtypes 岗位类型json数据
     * @return json
     */
    @RequestMapping("/batchdeljobtype")
    public Map<String, Object> batchdeljobtype(String[] jobtypes) {
        Map<String, Object> map = new HashMap<>();
        try {
            Integer[] jobtypeids = new Integer[jobtypes.length];
            for (int i = 0; i < jobtypes.length; i++) {
                jobtypeids[i] = Integer.parseInt(jobtypes[i]);
            }
            int res = jobTypeService.batchdel(jobtypeids);
            if (res > 0) {
                map.put("code", 200);
                map.put("msg", "操作成功");
            } else {
                map.put("code", 500);
                map.put("msg", "操作成功");
            }
        } catch (Exception e) {
            map.put("code", 500);
            map.put("msg", "操作成功");
            e.printStackTrace();
        }
        return map;
    }

    /**
     * 删除岗位数据
     *
     * @param typeid 岗位id
     * @return json
     */
    @RequestMapping("/deljobtypebyid")
    public Map<String, Object> deljobtypebyid(int typeid) {
        Map<String, Object> map = new HashMap<>();
        try {
            int res = jobTypeService.deleteByPrimaryKey(typeid);
            if (res > 0) {
                map.put("code", 200);
                map.put("msg", "删除成功");
            } else {
                map.put("code", 201);
                map.put("msg", "删除失败");
            }
        } catch (Exception e) {
            map.put("code", 500);
            map.put("msg", "删除失败");
        }
        return map;
    }

    /**
     * 更新岗位类型数据
     *
     * @param jobtypejson
     * @return
     */
    @RequestMapping("/updatejobtype")
    public Map<String, Object> updatejobtype(String jobtypejson) {
        Map<String, Object> map = new HashMap<>();
        try {
            JobType jobType = JSON.parseObject(jobtypejson, JobType.class);
            int res = jobTypeService.updateByPrimaryKey(jobType);
            if (res > 0) {
                map.put("code", 200);
                map.put("msg", "修改成功");
            } else {
                map.put("code", 201);
                map.put("msg", "修改失败");
            }
        } catch (Exception e) {
            map.put("code", 500);
            map.put("msg", "修改失败");
        }
        return map;
    }

    /**
     * 发布岗位
     *
     * @param jobjson 岗位json数据
     * @return 通用json返回格式
     */
    @RequestMapping(value = "/postJob")
    @ResponseBody
    public Map<String, Object> postJob(String jobjson) {
        Map<String, Object> map = new HashMap<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            EnterpriseJob job = JSON.parseObject(jobjson, EnterpriseJob.class);
            String enterpinyin = Pinyin4jUtil.getFirstSpellPinYin(job.getEnterprise().getEnterprisename(), false);
            String jobpinyin = Pinyin4jUtil.getFirstSpellPinYin(job.getJobname(), false);
            String jobid = enterpinyin + jobpinyin + CustomTools.getRandom4(3);
            job.setJobid(jobid);
            job.setPushtime(sdf.format(new Date()));
            int i = jobService.insertSelective(job);
            if (i > 0) {
                map.put("code", 200);
                map.put("msg", "岗位发布成功");
            } else {
                map.put("code", 500);
                map.put("msg", "岗位发布失败");
            }
        } catch (Exception e) {
            map.put("code", 500);
            map.put("msg", "system error");
            e.printStackTrace();
        }
        return map;
    }

    /**
     * 查询已发布岗位
     *
     * @param priseid  企业ID
     * @param pageNum  页数
     * @param pageSize 每页数目
     * @return 通用json返回格式
     */
    @RequestMapping("/getjobListbypriseid")
    @ResponseBody
    public Map<String, Object> getjobListbypriseid(String priseid, @RequestParam(defaultValue = "1") int pageNum, int pageSize) {
        Map<String, Object> map = new HashMap<>();
        try {
            //分页查询
            Page startPage = PageHelper.startPage(pageNum, pageSize);
            List<EnterpriseJob> enterpriseJobList = jobService.getjobListbypriseId(priseid);
            if (enterpriseJobList != null) {
                if (enterpriseJobList.size() > 0) {
                    map.put("code", 200);
                    map.put("msg", "岗位数据查询成功");
                    map.put("count", startPage.getTotal());
                    map.put("data", enterpriseJobList);
                } else {
                    map.put("code", 200);
                    map.put("msg", "你还未发布岗位");
                    map.put("data", enterpriseJobList);
                }
            } else {
                map.put("code", 500);
                map.put("msg", "岗位数据查询失败");
                map.put("data", null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            map.put("code", 500);
            map.put("msg", "system error");
            map.put("data", null);
        }
        return map;
    }

    /**
     * 模糊查询已发布岗位
     *
     * @param priseid  企业ID
     * @param jobname  岗位名字
     * @param pageNum  页数
     * @param pageSize 每页数目
     * @return 通用json返回格式
     */
    @RequestMapping("/getjobListbyjobname")
    @ResponseBody
    public Map<String, Object> getjobListbyjobname(String priseid, @RequestParam(defaultValue = "1") int pageNum, int pageSize, String jobname) {
        Map<String, Object> map = new HashMap<>();
        try {
            //分页查询
            Page startPage = PageHelper.startPage(pageNum, pageSize);
            List<EnterpriseJob> enterpriseJobList = jobService.getjobListbyjobname(priseid, jobname);
            if (enterpriseJobList != null) {
                if (enterpriseJobList.size() > 0) {
                    map.put("code", 200);
                    map.put("msg", "岗位数据查询成功");
                    map.put("count", startPage.getTotal());
                    map.put("data", enterpriseJobList);
                } else {
                    map.put("code", 500);
                    map.put("msg", "你还未发布岗位");
                    //得到总页数
                    map.put("data", enterpriseJobList);
                }
            } else {
                map.put("code", 500);
                map.put("msg", "岗位数据查询失败");
                map.put("data", null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            map.put("code", 500);
            map.put("msg", "system error");
            map.put("data", null);
        }
        return map;
    }

    /**
     * 通过岗位ID删除对应岗位数据
     *
     * @param jobid 岗位ID
     * @return 通用json返回格式
     */
    @RequestMapping("/deleteJob")
    @ResponseBody
    public Map<String, Object> deleteJob(String jobid) {
        Map<String, Object> map = new HashMap<>();
        int delcnt = jobService.deleteByPrimaryKey(jobid);
        if (delcnt > 0) {
            map.put("code", 200);
            map.put("msg", "删除成功!");
        } else {
            map.put("code", 500);
            map.put("msg", "删除失败！");
        }
        return map;
    }

    /**
     * 根据jobID查询岗位信息
     *
     * @param jobid 岗位ID
     * @return 通用json返回格式
     */
    @RequestMapping("/getjobdetailbyid")
    public Map<String, Object> getjobdetailbyid(String jobid) {
        Map<String, Object> map = new HashMap<>();
        try {
            EnterpriseJob job = jobService.getjobdetailbyid(jobid);
            if (job != null) {
                map.put("code", 200);
                map.put("msg", "数据请求成功");
                map.put("data", job);
            } else {
                map.put("code", 500);
                map.put("msg", "数据请求失败");
                map.put("data", null);
            }
        } catch (Exception e) {
            map.put("code", 500);
            map.put("msg", "数据请求失败");
            map.put("data", null);
            e.printStackTrace();
        }
        return map;
    }

    /**
     * 查询企业详细信息
     *
     * @param priseid 企业id
     * @return 通用json格式
     */
    @RequestMapping("/getcompanyDetail")
    public Map<String, Object> getcompanyDetail(String priseid) {
        Map<String, Object> map = new HashMap<>();
        try {
            Enterprise enterprise = enterpriseService.getcompanyDetail(priseid);
            if (enterprise != null) {
                map.put("code", 200);
                map.put("msg", "数据请求成功");
                map.put("data", enterprise);
            } else {
                map.put("code", 500);
                map.put("msg", "数据请求失败");
                map.put("data", null);
            }
        } catch (Exception e) {
            map.put("code", 500);
            map.put("msg", "数据请求失败");
            map.put("data", null);
            e.printStackTrace();
        }
        return map;
    }

    /**
     * 查询企业详细信息和对应岗位数量
     *
     * @param priseid 企业id
     * @return 通用json格式
     */
    @RequestMapping("/getcompanyHead")
    public Map<String, Object> getcompanyHead(String priseid) {
        Map<String, Object> map = new HashMap<>();
        try {
            Enterprise enterprise = enterpriseService.selectByPrimaryKey(priseid);
            List<EnterpriseJob> enterpriseJobList = jobService.getjobListbypriseId(priseid);
            int jobcnt = enterpriseJobList.size();
            if (enterprise != null) {
                map.put("code", 200);
                map.put("msg", "数据请求成功");
                map.put("jobcnt", jobcnt);
                map.put("data", enterprise);
            } else {
                map.put("code", 500);
                map.put("msg", "数据请求失败");
                map.put("data", null);
            }
        } catch (Exception e) {
            map.put("code", 500);
            map.put("msg", "数据请求失败");
            map.put("data", null);
            e.printStackTrace();
        }
        return map;
    }

    /**
     * 查询留言记录
     *
     * @param recordid 应聘记录id
     * @return json
     */
    @RequestMapping("/findReplysbyrecordid")
    public Map<String, Object> findReplysbyrecordid(int recordid) {
        Map<String, Object> map = new HashMap<>();
        try {
            List<Reply> replyList = replyService.findreplysbyrecordid(recordid);
            if (replyList != null) {
                map.put("code", 200);
                map.put("msg", "数据请求成功");
                map.put("data", replyList);
            } else {
                map.put("code", 201);
                map.put("msg", "数据请求失败");
                map.put("data", null);
            }

        } catch (Exception e) {
            map.put("code", 500);
            map.put("msg", "system error");
            map.put("data", null);
            e.printStackTrace();
        }
        return map;
    }

    /**
     * 添加留言
     *
     * @param replyjson 留言json
     * @return json
     */
    @RequestMapping("/addleaveAMessage")
    public Map<String, Object> addleaveAMessage(String replyjson) {
        Map<String, Object> map = new HashMap<>();
        try {
            Reply reply = JSONObject.parseObject(replyjson, Reply.class);
            SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
            reply.setReplytime(sf.format(new Date()));
            int i = replyService.insertSelective(reply);
            if (i > 0) {
                map.put("code", 200);
                map.put("msg", "留言成功");
            } else {
                map.put("code", 201);
                map.put("msg", "留言失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            map.put("code", 500);
            map.put("msg", "system error");
        }
        return map;
    }

}
